<html>
<head><meta charset="utf-8"><title>NonMax types? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html">NonMax types?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="226256265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226256265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Widua <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226256265">(Feb 13 2021 at 17:21)</a>:</h4>
<p>If someone (Me) were to implement a type like <code>NonMaxUsize</code>, analogous to <code>NonZeroUsize</code> but it can not be <code>usize::MAX</code> would it be welcome in std?</p>



<a name="226256335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226256335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Widua <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226256335">(Feb 13 2021 at 17:22)</a>:</h4>
<p>I sometimes need that for optional indices where I also need the 0th index.</p>



<a name="226256549"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226256549" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226256549">(Feb 13 2021 at 17:26)</a>:</h4>
<p>you can now do <code>pub struct UsizeExcept&lt;const NICHE: usize&gt;(NonZeroUsize);</code></p>



<a name="226256556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226256556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226256556">(Feb 13 2021 at 17:27)</a>:</h4>
<p>that seems preferable over adding a type per desirable niche</p>



<a name="226256560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226256560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226256560">(Feb 13 2021 at 17:27)</a>:</h4>
<p>and doesn't need to be in libstd code either</p>



<a name="226256849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226256849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Widua <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226256849">(Feb 13 2021 at 17:32)</a>:</h4>
<p>oh right I didn't think of that. Thank you :)</p>



<a name="226277402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226277402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226277402">(Feb 14 2021 at 00:36)</a>:</h4>
<p>Yeah, the general problem is that there's at least dozens of such types that could be interesting. <code>USizeButWithoutTheHighBit</code> represents the possible lengths of (non-ZST) slices, for example.  Or emulating HRESULT in a <code>Result</code> would like <code>PostiveI32</code> and <code>NegativeI32</code>.  And ...</p>



<a name="226297391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226297391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226297391">(Feb 14 2021 at 10:27)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/122651-general/topic/NonMax.20types.3F/near/226256549">said</a>:</p>
<blockquote>
<p>you can now do <code>pub struct UsizeExcept&lt;const NICHE: usize&gt;(NonZeroUsize);</code></p>
</blockquote>
<p>You'd still need <code>#[rustc_layout_scalar_valid_range_start(i)]</code>and friends.</p>



<a name="226297470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226297470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226297470">(Feb 14 2021 at 10:29)</a>:</h4>
<p>You don't need those if you're going to subtract, although there are a lot of use cases that aren't served that way, admittedly.</p>



<a name="226407018"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226407018" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226407018">(Feb 15 2021 at 16:48)</a>:</h4>
<p>Yeah if the type doesn't actually place the niche pattern where it belongs, and i have to add/subtract into and out of the type, then it's just not that helpful at all.</p>



<a name="226420170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226420170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226420170">(Feb 15 2021 at 19:06)</a>:</h4>
<p>See also <a href="https://stackoverflow.com/q/30414068/155423">Can I use the “null pointer optimization” for my own non-pointer types?</a></p>



<a name="226420691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/NonMax%20types%3F/near/226420691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/NonMax.20types.3F.html#226420691">(Feb 15 2021 at 19:11)</a>:</h4>
<p>I usually use newtypes for this kind of thing, which make it easy to get the expected performance and size. The API usually doesn't have to be very large either, just a few accessors</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[derive(Copy, Clone)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Watch</span><span class="p">(</span><span class="kt">usize</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Watch</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">  </span><span class="cp">#[inline]</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">(</span><span class="n">idx</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">mask</span>: <span class="kt">bool</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Watch</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Watch</span><span class="p">(</span><span class="n">idx</span><span class="w"> </span><span class="o">&lt;&lt;</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">mask</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">)</span><span class="w"></span>
<span class="w">  </span><span class="p">}</span><span class="w"></span>
<span class="w">  </span><span class="cp">#[inline]</span><span class="w"> </span><span class="k">fn</span> <span class="nf">clause</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"> </span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">  </span><span class="cp">#[inline]</span><span class="w"> </span><span class="k">fn</span> <span class="nf">mark</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"> </span><span class="o">|=</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w">  </span><span class="cp">#[inline]</span><span class="w"> </span><span class="k">fn</span> <span class="nf">marked</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="mi">0</span><span class="w"> </span><span class="o">&amp;</span><span class="w"> </span><span class="mi">1</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>